package com.ruoyi.patient.service;


import com.ruoyi.patient.entity.LoginPatient;
import com.ruoyi.patient.entity.AddressBody;
import com.ruoyi.service.domain.ServiceAddress;
import com.ruoyi.service.mapper.ServiceAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class AddressService {

    @Autowired
    private ServiceAddressMapper addressMapper;


    @Transactional
    public boolean addAddress(AddressBody addressBody, LoginPatient oldPatient)
    {
        try {
            addressBody.setAddressId(null);
            addressBody.setAddressUserid(oldPatient.getUserId());
            addressBody.setAddressUpdateBy(oldPatient.getUsername());
            addressBody.setAddressCreateBy(oldPatient.getUsername());
            addressBody.setAddressCreateTime(new Date());
            addressBody.setAddressUpdateTime(new Date());
            addressMapper.insertServiceAddress(addressBody);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Transactional
    public boolean updateAddress(AddressBody addressBody, LoginPatient oldPatient)
    {
        try {
            addressBody.setAddressUpdateBy(oldPatient.getUsername());
            addressBody.setAddressUpdateTime(new Date());
            addressMapper.updateServiceAddress(addressBody);
        }catch (Exception e){
            return false;
        }
        return true;
    }

    public List<ServiceAddress> getAddress(AddressBody addressBody, LoginPatient oldPatient)
    {
        addressBody.setAddressUserid(oldPatient.getUserId());
        List<ServiceAddress> retAddressList = addressMapper.selectServiceAddressList(addressBody);
        return retAddressList;
    }




}
